import request from '@/utils/request'
import type { ResponseData } from '@/types/common'
import type { Order, AdditionalServices } from '@/types/order'

// 获取订单列表
export const getOrders = (params?: {
  keyword?: string
  status?: number
  payment_status?: number
  start_date?: string
  end_date?: string
  page?: number
  pageSize?: number
  photographer_id?: number  // 新增摄影师ID参数
}) => {
  return request.get<{
    code: number
    data: {
      items: Order[]
      meta: {
        total: number
        page: number
        pageSize: number
        totalPage: number
      }
    }
    message: string
  }>('/orders/list', {
    params: {
      keyword: params?.keyword || '',
      status: params?.status,
      payment_status: params?.payment_status,
      start_date: params?.start_date,
      end_date: params?.end_date,
      page: params?.page || 1,
      pageSize: params?.pageSize || 10,
      photographer_id: params?.photographer_id  // 添加摄影师ID参数传递
    },
  })
}

// 获取订单详情
export const getOrderDetail = (id: number) => {
  return request.get<{
    code: number
    data: Order
    message: string
  }>(`/orders/${id}/detail`)
}

// 取消订单
export const cancelOrder = (id: number) => {
  return request.patch<ResponseData>(`/orders/${id}/cancel`)
}

// 支付订单
export const payOrder = (id: number, amount: number) => {
  return request.post<ResponseData>(`/orders/${id}/pay`, { amount })
}

// 退款
export const refundOrder = (id: number, amount: number, reason?: string) => {
  return request.post<ResponseData>(`/orders/${id}/refund`, { amount, reason })
}

// 确认订单
export const confirmOrder = (data: {
  orderId: number
  additionalServices: AdditionalServices
  totalAmount: number
}) => {
  return request.post<ResponseData>(`/orders/confirm`, data)
}

// 完成订单
export const completeOrder = (id: number) => {
  return request.patch<ResponseData>(`/orders/${id}/complete`)
}

// 已收定金
export const confirmDeposit = (id: number) => {
  return request.patch<ResponseData>(`/orders/${id}/confirm-deposit`)
}

// 已收尾款
export const confirmFinalPayment = (id: number) => {
  return request.patch<ResponseData>(`/orders/${id}/confirm-final-payment`)
}
